/**
 * Created by Administrator on 2017/4/11.
 */
"use strict";
swan_pc
    .controller("OrgOppListController",function($scope,$rootScope,$cookieStore,$state,ngDialog,myOrgContact,CompanyInfo,opportunityByCanSee,oppBase,orgCompanyList,tagSetByOrg,productNew,reportList) {

        $scope.width12=((document.body.clientWidth-60)/12*10-35)/11;
        $scope.width7=((document.body.clientWidth-60)/12*10-35)/7;
        var _user=$cookieStore.get("user");
        $cookieStore.put('_currentOrganizationId',_user.data.organizationId)
        $rootScope.organizationId=_user.data.organizationId;
        $rootScope.userId=_user.data.id

        myOrgContact.get({id:$rootScope.userId},function (result) {
            $scope.myOrgContact=result;
        })

        $scope.isMyOpp=false;

        $scope.chooseProvince=null;
        $scope.chooseCity=null;
        $scope.chooseDistrict=null;
        $scope.chooseStreet=null;

        $scope.startYear=null;
        $scope.startSeason=null;
        $scope.endYear=null;
        $scope.endSeason=null;

        $scope.seasonList = [
            {
                name:"第一季度",
                value:1
            },
            {
                name:"第二季度",
                value:2
            },
            {
                name:"第三季度",
                value:3
            },
            {
                name:"第四季度",
                value:4
            }
        ];

        $scope.generateYearList = function(){
            $scope.yearList = [];
            for(var i=2000;i<2021;i++){
                $scope.yearList.push(i);
            }
        };
        $scope.generateYearList();

        $scope.resetSeason=function () {
            $scope.season=false;
            $scope.startYear=null;
            $scope.startSeason=null;
            $scope.endYear=null;
            $scope.endSeason=null;
        };


        $scope.getAllOpp=function () {
            opportunityByCanSee.query({page: 0, size: 1000, sort: 'asc',id:_user.data.id},function (result) {
                $scope.oppList=result;
                console.log("OPP LIST",$scope.oppList);
                $scope.initPageSort(result);
                $scope.getOther();
            })
        };

        $scope.getAllOpp();

        $scope.selectOne=function (item) {
            $rootScope.opp=item;
            $state.go('orgOppBase');
        }


        //分页控制实现
        $scope.pageSize=10;
        $scope.initPageSort=function(item){
            $scope.data = item;
            $scope.pages = Math.ceil($scope.data.length / $scope.pageSize); //分页数
            $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
            $scope.pageList = [];
            $scope.selPage = 1;
            //设置表格数据源(分页)
            $scope.setData = function () {
                $scope.items = $scope.data.slice(($scope.pageSize * ($scope.selPage - 1)), ($scope.selPage * $scope.pageSize));//通过当前页数筛选出表格当前显示数据
            };
            $scope.items = $scope.data.slice(0, $scope.pageSize);
            //分页要repeat的数组
            for (var i = 0; i < $scope.newPages; i++) {
                $scope.pageList.push(i + 1);
            }
            //打印当前选中页索引
            $scope.selectPage = function (page) {
                //不能小于1大于最大
                if (page < 1 || page > $scope.pages) return;
                //最多显示分页数5
                if (page > 2) {
                    //因为只显示5个页数，大于2页开始分页转换
                    var newpageList = [];
                    for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)); i++) {
                        newpageList.push(i + 1);
                    }
                    $scope.pageList = newpageList;
                }
                $scope.selPage = page;
                $scope.setData();
                $scope.isActivePage(page);
                //console.log("选择的页：" + page);
            }
        };
        //设置当前选中页样式
        $scope.isActivePage = function (page) {
            return $scope.selPage == page;
        };
        //上一页
        $scope.Previous = function () {
            $scope.selectPage($scope.selPage - 1);
        };
        //下一页
        $scope.Next = function () {
            $scope.selectPage($scope.selPage + 1);
        };

        $scope.returnAllListLength = function(){
            return $scope.oppList.length;
        };

        // 排序
        $scope.orderByKey=function (key) {
            $scope.reverse=($scope.orderKey==key)?!$scope.reverse:false;
            $scope.orderKey=key;
        };
        $scope.orderList=function (item) {
            return item[$scope.orderKey];
        };


        $scope.$watch(function () {
            return $scope.searchInfo;
        },function () {
            if ($scope.searchInfo!=null&&$scope.searchInfo!=''){
                var newList=[];
                _.forEach($scope.oppList,function (data) {
                    if (data.name.indexOf($scope.searchInfo)>=0||data.customName.indexOf($scope.searchInfo)>=0){
                        newList.push(data)
                    }
                })
                $scope.initPageSort(newList)
            }else{
                if($scope.oppList!=null && $scope.oppList.length>0){
                    $scope.initPageSort($scope.oppList);
                }
            }
        })

        $scope.goClassify = function(){
            oppBase.get({id:$rootScope.opp.id},function (result) {
                $scope.presentInfo = result;
                $scope.goSref('orgOppClassify');
            });
        };

        $scope.getState=function (num) {
            return $scope.stateList[num].name;
        }
        $scope.getStatus=function (num) {
            return $scope.statusList[num].name;
        }

        $scope.resetOption=function () {
            $scope.isFilter=false;

            $scope.institution=false;
            $scope.report=false;
            $scope.customer=false;
            $scope.area=false;
            $scope.location=false;
            $scope.product=false;
            $scope.tag=false;
            $scope.partner=false;
            $scope.enemy=false;
            $scope.season=false;
            $scope.status=false;
            $scope.state=false;
            $scope.before=false;
            $scope.nowAll=false;
            $scope.nowIn=false;
            $scope.nextIn=false;
            $scope.future=false;

            $scope.startYear=null;
            $scope.startSeason=null;
            $scope.endYear=null;
            $scope.endSeason=null;


            $scope.getAllOpp();
        };

        $scope.getOther=function () {
            $scope.filterList=['汇报链','客户','地域','地点','产品线','标签','合作伙伴','竞争对手','财季','状况','状态']
            $scope.customList=[];
            $scope.reportList=[];
            $scope.areaList=[
                {
                    name:'华北',
                    value:1,
                    select:false
                },
                {
                    name:'东北',
                    value:2,
                    select:false
                },
                {
                    name:'华南',
                    value:3,
                    select:false
                },
                {
                    name:'西北',
                    value:4,
                    select:false
                },
                {
                    name:'西部',
                    value:5,
                    select:false
                },
                {
                    name:'中部',
                    value:6,
                    select:false
                },
                {
                    name:'华东',
                    value:7,
                    select:false
                }
            ];
            $scope.address=[];
            $scope.productList=[];
            $scope.tagList=[];
            $scope.partnerList=[];
            $scope.enemyList=[];
            $scope.stateList=[
                {
                    name:'策划',
                    value:0,
                    select:false,
                },
                {
                    name:'准备',
                    value:1,
                    select:false,
                },
                {
                    name:'招标',
                    value:2,
                    select:false,
                },
                {
                    name:'评标',
                    value:3,
                    select:false,
                },
                {
                    name:'谈判',
                    value:4,
                    select:false,
                },
                {
                    name:'交付',
                    value:6,
                    select:false,
                },
            ];
            $scope.statusList=[
                {
                    name:'开放',
                    value:0,
                    select:false,
                },
                {
                    name:'赢得',
                    value:1,
                    select:false,
                },
                {
                    name:'失败',
                    value:2,
                    select:false,
                },
                {
                    name:'没有机会',
                    value:3,
                    select:false,
                },
                {
                    name:'取消',
                    value:4,
                    select:false,
                },
                {
                    name:'关闭',
                    value:5,
                    select:false,
                },
            ];

            console.log("USERID",$rootScope.userId)
            orgCompanyList.query({id:$rootScope.userId},function(result){
                _.forEach(result,function (item) {
                    $scope.customList.push({
                        name:item.companyBaseInfo.name,
                        value:item.id,
                        select:false
                    })
                    $scope.partnerList.push({
                        name:item.companyBaseInfo.name,
                        value:item.id,
                        select:false
                    });
                    $scope.enemyList.push({
                        name:item.companyBaseInfo.name,
                        value:item.id,
                        select:false
                    });
                })
            });

            tagSetByOrg.query({id:$rootScope.organizationId},function(result){
                _.forEach(result,function (item) {
                    _.forEach(item.tagList,function (x) {
                        $scope.tagList.push({
                            name:x.name,
                            value:x.id,
                            select:false
                        })
                    })
                })
            });

            productNew.query({page:0,size:5000,sort:'asc'},function (result) {
                _.forEach(result,function (item) {
                    $scope.productList.push({
                        name:item.name,
                        value:item.id,
                        select:false
                    })
                })
            })

            reportList.query({page:0,size:5000,sort:'asc',id:$scope.myOrgContact.id},function (result) {
                _.forEach(result,function (item) {
                    if (item.friendId!=null&&item.id!=$scope.myOrgContact.id){
                        $scope.reportList.push({
                            name:item.baseInfo.name,
                            value:item.friendId,
                            select:false
                        })
                    }
                })
                console.log("MY",result,$scope.reportList)
            })

        }


        $scope.initChooseAddess = function(){
            $scope.choseAddress={
                province:null,
                city:null,
                district:null,
                street:null
            }
        };
        $scope.initChooseAddess();

        $scope.setFilter=function (num) {

            $scope.filterName=$scope.filterList[num];
            ngDialog.open({
                templateUrl:'scripts/components/organizationBusiness/orgOpp/selectFilter.html',
                scope:$scope,
                controller:["$scope",function(){
                    console.log("run");
                    $scope.type=num;

                }]
            })
        };

        $scope.chooseCustom=function (custom) {
            var tag=false;
            _.forEach($scope.customList,function (item) {
                if(item.value==custom.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.customer=tag;
        }

        $scope.chooseReport=function (report) {
            var tag=false;
            _.forEach($scope.reportList,function (item) {
                if(item.value==report.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.report=tag;
        }

        $scope.chooseArea=function (area) {
            var tag=false;
            _.forEach($scope.areaList,function (item) {
                if(item.value==area.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.area=tag;

        }


        $scope.chooseProduct=function (product) {
            var tag=false;
            _.forEach($scope.productList,function (item) {
                if(item.value==product.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.product=tag;

        }

        $scope.chooseTag=function (tag1) {
            var tag=false;
            _.forEach($scope.tagList,function (item) {
                if(item.value==tag1.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.tag=tag;

        }

        $scope.choosePartner=function (partner) {
            var tag=false;
            _.forEach($scope.partnerList,function (item) {
                if(item.value==partner.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.partner=tag;

        }

        $scope.chooseEnemy=function (enemy) {
            var tag=false;
            _.forEach($scope.enemyList,function (item) {
                if(item.value==enemy.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.enemy=tag;

        }

        $scope.chooseState=function (state) {
            var tag=false;
            _.forEach($scope.stateList,function (item) {
                if(item.value==state.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.state=tag;

        }

        $scope.chooseStatus=function (status) {
            var tag=false;
            _.forEach($scope.statusList,function (item) {
                if(item.value==status.value){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }
            })

            $scope.status=tag;

        }

        $scope.startFilter=function (item) {
            var result=false,report=false,season=false,custom=false,area=false,product=false,partner=false,enemy=false,state=false,tag=false,status=false,location=false;
            if ($scope.report){
                _.forEach($scope.reportList,function (x) {
                    if(x.select&&x.value==item.ownerId){
                        report=true
                    }
                })
            }
            if ($scope.customer){
                _.forEach($scope.customList,function (x) {
                    if(x.select&&x.value==item.customId){
                        custom=true
                    }
                })
            }

            if ($scope.area){
                _.forEach($scope.areaList,function (x) {
                    if(x.select&&x.value==item.region)
                        area=true;
                })
            }

            if ($scope.state){
                _.forEach($scope.stateList,function (x) {
                    if(x.select&&x.value==item.state)
                        state=true;
                })
            }

            if ($scope.status){
                _.forEach($scope.statusList,function (x) {
                    if(x.select&&x.value==item.status)
                        status=true;
                })
            }

            if ($scope.product){
                _.forEach($scope.productList,function (x) {
                    if (x.select){
                        _.forEach(item.products,function (y) {
                            if (x.value==y.id)
                                product=true;
                        })
                    }
                })
            }

            if ($scope.tag){
                _.forEach($scope.tagList,function (x) {
                    if (x.select){
                        _.forEach(item.tags,function (y) {
                            if (x.value==y.id)
                                tag=true;
                        })
                    }
                })
            }

            if ($scope.partner){
                _.forEach($scope.partnerList,function (x) {
                    if (x.select){
                        _.forEach(item.partnerDTOs,function (y) {
                            if (x.value==y.orgCompany.id)
                                partner=true;
                        })
                    }
                })
            }

            if ($scope.enemy){
                _.forEach($scope.enemyList,function (x) {
                    if (x.select){
                        _.forEach(item.enemyDTOs,function (y) {
                            if (x.value==y.orgCompany.id)

                                enemy=true;
                        })
                    }
                })
            }

            // 财季
            if ($scope.startYear!=null&&$scope.endYear!=null&&$scope.startSeason!=null&&$scope.endSeason!=null){
                $scope.season=true;
                if (item.actualDate!=null){
                    var date =new Date(item.actualDate);
                    var seasons=date.getFullYear()*4+date.getMonth()/3+1
                    var start=$scope.startYear*4+$scope.startSeason;
                    var end=$scope.endYear*4+$scope.endSeason;
                    if (seasons>=start&&seasons<=end){
                        season=true;
                    }
                }
            }

            // 地点
            if ($scope.choseAddress.province!=null){
                // console.log('choose locationTTTTTTTTTT',$scope.chooseProvince,$scope.chooseCity,$scope.chooseDistrict,$scope.chooseStreet)

                $scope.location=true;
                if(item.province==$scope.choseAddress.province){
                    if($scope.choseAddress.city==''||item.city==$scope.choseAddress.city){
                        if ($scope.choseAddress.district==''||item.district==$scope.choseAddress.district){
                            if ($scope.choseAddress.street==''||item.street==$scope.choseAddress.street){
                                location=true;
                            }
                        }
                    }
                }
            }

            result=(!$scope.report||report)&&(!$scope.customer||custom)&&(!$scope.area||area)&&(!$scope.state||state)&&(!$scope.tag||tag)&&(!$scope.partner||partner)
                &&(!$scope.enemy||enemy)&&(!$scope.status||status)&&(!$scope.location||location)&&(!$scope.product||product)&&(!$scope.season||season);
            return result;
        }
    })